<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Item;
use App\Models\User;
use Illuminate\Http\Request;
use Validator;

class ItemController extends Controller {
	private $role_flag;
	private $user;
	public function __construct() {
		if (\Auth::guard('api')->check()) {
			$getUser = \Auth::guard('api')->user();
			$user = User::find($getUser->id);
			if (!$this->getDevice()) {
				//app
				$user->company_id = $user->appcompany_id;
			}

			$this->user = $user;
			$company = \App\Models\Company::find($this->user->company_id);
			$this->role_flag = $company->role_flag;
		}
	}
	public function get(Request $request, $id) {
		$data = Item::find($id);
		if (!empty($data)) {
			return $this->respond('get_ok', $data);
		}
		return $this->respond('get_error');
	}
	//新增保存
	public function add(Request $request) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		$roleType = $request->input('roleType');
		if ($roleType == 1) {
			if (!empty($staff) && $staff->company_id == $user->company_id) {
				if (!$user->can('resume_edit') && !$user->can('resume_add') && !$user->hasRole('管理员')) {
					return $this->respond('no_auth');
				}
			} else {
				if (!$user->can('resume_edit') && !$user->can('resume_add')) {
					return $this->respond('no_auth');
				}
			}
			return $this->respond('op_ok');
		}
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('resume_edit') && !$user->can('resume_add') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('resume_edit') && !$user->can('resume_add')) {
				return $this->respond('no_auth');
			}
		}
		$validator = Validator::make($request->all(), Item::$rules, Item::$message);
		if ($validator->fails()) {
			return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
		}
		$m = new Item();
		$resumeId = $request->input('resumeId');
		if (empty($resumeId)) {
			return response()->json(['code' => 500, 'msg' => '简历id不能为空']);
		}
		$m->resume_id = $resumeId;
		$m->name = $request->input('name');
		$m->url = $request->input('url');
		$m->role = $request->input('role');
		$m->discription = $request->input('discription');
		$m->harvest = $request->input('harvest');
		$m->start_date = $request->input('startDate');
		$m->end_date = $request->input('endDate');
		$result = $m->save();
		if ($result) {
			return $this->respond('add_ok');
		}
		return $this->respond('add_error');
	}
	//更新保存
	public function put(Request $request, $id) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		$roleType = $request->input('roleType');
		if ($roleType == 1) {
			if (!empty($staff) && $staff->company_id == $user->company_id) {
				if (!$user->can('resume_edit') && !$user->hasRole('管理员')) {
					return $this->respond('no_auth');
				}
			} else {
				if (!$user->can('resume_edit')) {
					return $this->respond('no_auth');
				}
			}
			return $this->respond('op_ok');
		}
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('resume_edit') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('resume_edit')) {
				return $this->respond('no_auth');
			}
		}
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '项目经验id不能为空']);
		}
		$validator = Validator::make($request->all(), Item::$rules, Item::$message);
		if ($validator->fails()) {
			return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
		}
		$model = new Item();
		$m = $model::find($id);
		if (empty($m)) {
			return $this->respond('get_error');
		}
		$m->name = $request->input('name');
		$m->url = $request->input('url');
		$m->role = $request->input('role');
		$m->discription = $request->input('discription');
		$m->harvest = $request->input('harvest');
		$m->start_date = $request->input('startDate');
		$m->end_date = $request->input('endDate');
		$result = $m->save();
		if ($result) {
			return $this->respond('update_ok');
		}
		return $this->respond('update_error');
	}
	//删除步骤
	public function remove(Request $request, $id) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('resume_edit') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('resume_edit')) {
				return $this->respond('no_auth');
			}
		}
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '项目经验id不能为空']);
		}
		$m = new Item();
		$model = $m::find($id);
		if (empty($model)) {
			return $this->respond('get_error');
		}
		$model->status = 1;
		$result = $model->save();
		if ($result) {
			return $this->respond('delete_ok');
		}
		return $this->respond('delete_error');
	}
}
